package uk.co.richardwkirk.parkrun.funnels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.SwingUtilities;

import uk.co.richardwkirk.parkrun.funnels.display.DisplaySettings;
import uk.co.richardwkirk.parkrun.funnels.display.applet.FunnelAppletDisplay;
import uk.co.richardwkirk.parkrun.funnels.model.RaceFinisherException;

public class FunnelApplet extends JApplet implements ActionListener, Runnable {

	private static final long serialVersionUID = 1L;
	
	FunnelAppletDisplay funnelDisplay = null;
	
    public void init() {
        setSize(600, 400);
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    createGUI();
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
        }
    }
    
    
    private void createGUI() {
        // Create and set up the content pane.
    	DisplaySettings displaySettings = new DisplaySettings();
        funnelDisplay = new FunnelAppletDisplay(displaySettings);
        setContentPane(funnelDisplay.createPanel(this));        
    }  
    
    
	@Override
	public void actionPerformed(ActionEvent arg0) {
		Thread t = new Thread(this);
		t.start();
	}


	@Override
	public void run() {
		FunnelSettings funnelSettings = new FunnelSettings();
		try {
			String dataSet = "http://www.parkrun.org.uk/bushy/results/weeklyresults?runSeqNumber=395";
			FunnelTest funnelTest = new FunnelTest(funnelSettings, dataSet);
			funnelTest.setDisplay(funnelDisplay);
			funnelTest.runTest();
		} catch (RaceFinisherException e) {
			System.err.println("Could not build list of race finishers.");
			e.printStackTrace(System.err);
		}		
	}
	
}
